//
//  SaveTool.m
//  大概是豆瓣
//
//  Created by 甘霞 on 15/10/12.
//  Copyright © 2015年 甘霞. All rights reserved.
//

#import "SaveTool.h"
#import "GXSuggestModel.h"

@implementation SaveTool

+ (BOOL)suggestCollections:(GXSuggestModel *)suggestModel {
    //获取沙盒路径
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"suggest.plist"];
    //进行反归档
    NSMutableArray *suggestArr = [NSKeyedUnarchiver unarchiveObjectWithFile:sandBoxPath];
    NSLog(@"沙盒路径:%@", sandBoxPath);
    //如果反归档返回的数组为空 则当前文件夹中没有对应文件 创建空数组 进行归档
    if (suggestArr == nil) {
        suggestArr = [NSMutableArray array];
        [suggestArr addObject:suggestModel];
        //归档
        [NSKeyedArchiver archiveRootObject:suggestArr toFile:sandBoxPath];
        NSLog(@"收藏成功");
        return YES;
    } else {
        //返回数组不为空 判断是否已经收藏此电影
        //遍历数组
        for (GXSuggestModel *model in suggestArr) {
            if ([suggestModel.title isEqualToString:model.title]) {
                //已经收藏/不重复收藏
                NSLog(@"已经收藏过");
                return NO;
            }
        }
        [suggestArr addObject:suggestModel];
        [NSKeyedArchiver archiveRootObject:suggestArr toFile:sandBoxPath];
        NSLog(@"收藏成功");
        return YES;
    }
}

@end
